package cn.italkcloud.cph.config.exception;

import cn.italkcloud.cph.config.sys.ResponseCode;
import cn.italkcloud.cph.constants.enums.ErrorEnum;
import cn.italkcloud.cph.constants.enums.FailedEnum;

/**
 * 业务异常
 *
 * @author dominate
 */
public class BusinessException extends RuntimeException {

    private final String exceptionCode;

    private final String exceptionMsg;

    public BusinessException(String message) {
        super(message);
        this.exceptionMsg = message;
        this.exceptionCode = ResponseCode.FAILED.getCodeStr();
    }

    public BusinessException(String exceptionCode, String exceptionMsg) {
        this.exceptionCode = exceptionCode;
        this.exceptionMsg = exceptionMsg;
    }

    public BusinessException(String message, String exceptionCode, String exceptionMsg) {
        super(message);
        this.exceptionCode = exceptionCode;
        this.exceptionMsg = exceptionMsg;
    }

    public BusinessException(String message, Throwable cause, String exceptionCode, String exceptionMsg) {
        super(message, cause);
        this.exceptionCode = exceptionCode;
        this.exceptionMsg = exceptionMsg;
    }

    public BusinessException(Throwable cause, String exceptionCode, String exceptionMsg) {
        super(cause);
        this.exceptionCode = exceptionCode;
        this.exceptionMsg = exceptionMsg;
    }

    public BusinessException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace, String exceptionCode, String exceptionMsg) {
        super(message, cause, enableSuppression, writableStackTrace);
        this.exceptionCode = exceptionCode;
        this.exceptionMsg = exceptionMsg;
    }

    public static BusinessException create(FailedEnum failed) {
        return new BusinessException(failed.getCode(), failed.getMessage());
    }

    public static BusinessException create(ErrorEnum error) {
        return new BusinessException(FailedEnum.COMMON.getCode(), error.getMessage());
    }

    public static BusinessException createMessage(String message) {
        return new BusinessException(FailedEnum.COMMON.getCode(), message);
    }

    public static BusinessException create(ResponseCode result) {
        return new BusinessException(result.getCodeStr(), result.getMsg());
    }

    public String getExceptionCode() {
        return exceptionCode;
    }

    public String getExceptionMsg() {
        return exceptionMsg;
    }
}
